package com.zhiyou100.www.bbs.login;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @packageName: ${NAME}
 * @className: ${PACKAGE_NAME}
 * @Description: TODO 退出登陆
 * @author: yang
 * @date: 2020/6/6
 */
@WebServlet(name = "LoggedOut", urlPatterns = "/LoggedOut")
public class LoggedOut extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        // 设定编码集
        response.setContentType("text/html;charset=UTF-8");
        // 设定类型

        HttpSession servletContext = request.getSession();
        servletContext.setAttribute("user",null);
        // 设置用户的属性为null

        Object loginNumber = servletContext.getAttribute("loginNumber");
        // 获取登陆当前登陆者数量

        servletContext.setAttribute("loginNumber",(Integer)(loginNumber)-1);
        // 登陆者的数量-1

        response.sendRedirect("/bbs/html/loginFailure.html");
        // 转发到 登陆页面

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
